{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T13:45:31.152418Z",
     "start_time": "2025-10-24T13:45:31.149616Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class Accumulator:\n",
    "    #这是类中的特殊方法，可以把传给对象的参数放到类的变量中\n",
    "    def __init__(self,n):\n",
    "        self.data = [0.0] * n\n",
    "        #构造函数\n",
    "\n",
    "    def add(self,*args):#类中的方法的第一个参数通常被命名为self，是指向实例的引用\n",
    "        #在类的方法中，可以通过self来访问和操作实例\n",
    "        self.data = [a + float(b) for a,b in zip(self.data,args)]\n",
    "        #将新传入的多个数值，分别累加到对应的已有数值上\n",
    "        #1、将新传入的args和已经有的data进行配对，就是把对应位置上的数据组合，形成一个包含元组的序列\n",
    "        #2、循环解包，每次循环取出一个元组，把元组里的两个数分别给a和b\n",
    "        #3、把解包出来的a和b进行累加\n",
    "        #4、更新self.data中对应位置的值\n",
    "    def print_value(self):\n",
    "        print(self.data)"
   ],
   "id": "ab2fa51ca5202ac4",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T13:45:31.161400Z",
     "start_time": "2025-10-24T13:45:31.158776Z"
    }
   },
   "cell_type": "code",
   "source": [
    "acc = Accumulator(2)\n",
    "acc.add(1,2)\n",
    "\n",
    "acc.print_value()\n",
    "acc.data"
   ],
   "id": "52f84ec498ac954f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.0, 2.0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1.0, 2.0]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#类的继承\n",
    "class Person:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "#继承就是在一个新的类里继承一个已经存在的类，减少开发的复杂度\n",
    "#可以继承多个类，多继承\n",
    "class Student(Person):\n",
    "    def __init__(self,name,age,grade):\n",
    "        Person.__init__(self,name,age)\n",
    "        self.grade = grade"
   ],
   "id": "f93bc1ec41059c84"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
